<?php
/**
 * Created by PhpStorm.
 * User: dreamy
 * Date: 2019-05-25
 * Time: 11:16
 */
namespace common\services;

use common\models\Tally;
use common\models\TallyPm;
use common\models\TallyRecord;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

/**
 * Class TallyService
 * @package common\services
 * 账本管理
 */
class TallyService extends BaseService
{
    /**
     * @param $uid
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getListByUid($uid,$condition=[])
    {
        // 先从缓存里尝试命中 TODO......
        $tallyIds = TallyPmService::getTallyIdsByUid($uid);
        $where = [
            'uid'   => $uid,
        ];
        if(ArrayHelper::getValue($condition,'type')){
            $where['type'] = $condition['type'] ; // 收入or支出
        }
        // 从DB获取
        $accountList = Tally::find()
            ->select('*')
            ->where($where)
            ->limit(100)
            ->asArray()
            ->all();
        return $accountList ;
    }

    /**
     * 获取某个账本信息
     * @param $id
     *
     * @return array|\yii\db\ActiveRecord|null
     */
    public static function getOne($id)
    {
        $where = [
            'id'    => $id,
        ];
        // 从DB获取
        $info = Tally::find()
            ->select('*')
            ->where($where)
            ->limit(1)
            ->one();
        return $info ;
    }

    /**
     * @param $id
     * @param $uid
     * @return array|\yii\db\ActiveRecord|null
     * @notes 获取某个用户下的某个账单详细数据
     */
    public static function getOneByUid($id,$uid='')
    {
        $where = [
            'id'    => $id,
        ];
        if($uid) {
            $where['uid'] = $uid;
        }
        // 从DB获取
        $info = TallyRecord::find()
            ->select('*')
            ->where($where)
            ->limit(1)
            ->one();
        return $info ;
    }

    /**
     * @param $formData
     * @return bool
     */
    public static function create($formData)
    {
        $model = new Tally();
        $model->load($formData,'');
        if ($model->validate() && $model->save()) {
            return $model->id ;
        }else{
            Yii::error('Tally insert error:'.current($model->getFirstErrors()).';post:'.Json::encode($formData,JSON_UNESCAPED_UNICODE));
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }

    /**
     * @param $condition
     * @param $formData
     * @return bool
     */
    public static function update($condition,$formData)
    {
        $model = Tally::find()->where($condition)->one();
        if(!$model){
            self::setError('账本不存在');
            return false ;
        }
        $model->load($formData,'');
        if ($model->validate() && $model->save()) {
            return true ;
        }else{
            Yii::error('Tally insert error:'.current($model->getFirstErrors()).';post:'.Json::encode($formData,JSON_UNESCAPED_UNICODE));
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }

    /**
     * @param $condition
     * @return bool
     * @throws
     */
    public static function delete($condition)
    {
        $model = Tally::find()->where(['id'=>$condition['id']])->one();
        if(!$model){
            self::setError('账本不存在');
            return false ;
        }
        // 只有创建者才可以删除账本
        if($model->uid != $condition['uid']) {
            self::setError('非创建者无法删除该账套');
            return false;
        }
        $model->state = 0 ; // 设置状态不可用
        if ($model->save()) {
            return true ;
        }else{
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }

}
